package cn.easy.mortgage.domain;

import cn.easy.base.domain.User;
import cn.easy.base.domain.core.AtEntity;
import cn.easy.mortgage.utils.Constants;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(name = "mtg_loan_request")
public class LoanRequest extends AtEntity {
    /**
     * 房产评估
     */
    private String estateEvalId;
    /**
     * 来源
     */
    private String channelId;
    /**
     * 姓名
     */
    private String name;
    /**
     * 手机号
     */
    private String mobile;
    /**
     * 贷款金额
     */
    private BigDecimal loanAmount;
    /**
     * 地区
     */
    private String area;
    /**
     * 备注
     */
    private String remark;
    /**
     * 状态
     */
    private Integer status;
    /**
     * 负责人
     */
    private String managerId;
    /**
     * 来源
     */
    private Integer source;
    /**
     * 房产购入时间
     */
    private Date boughtAt;
    /**
     * 产权共有人
     */
    private Integer propertyCoOwner;
    /**
     * 唯一住房
     */
    private Integer isOnlyHouse;
    /**
     * 当前抵押状态
     */
    private Integer mortgageStatus;
    /**
     * 住房使用状态
     */
    private Integer useStatus;
    /**
     * 婚姻状态
     */
    private Integer maritalStatus;
    /**
     * 2年内是否逾期
     */
    private Integer isOverdue;
    /**
     * 逾期次数
     */
    private Integer overdueCount;
    /**
     * 资金用途
     */
    private String fundUse;
    /**
     * 一级渠道
     */
    private String channelNo;
    /**
     * 电销经理
     */
    private String telSaleManager;

    private EstateEvalInfo estateEvalInfo;

    private Channel channel;

    private User user;

    public static LoanRequest valueOfH5(String regCode, String mobile, EstateEvalInfo _EstateEvalInfo, Channel channel) {
        return LoanRequest.valueOf(regCode, mobile, _EstateEvalInfo, channel, Constants.REQUEST_SOURCE_H5);
    }

    public static LoanRequest valueOfMiniApp(String regCode, String mobile, EstateEvalInfo _EstateEvalInfo, Channel channel) {
        return LoanRequest.valueOf(regCode, mobile, _EstateEvalInfo, channel, Constants.REQUEST_SOURCE_MINI_APP);
    }

    public static LoanRequest valueOfZxy(String regCode, String mobile, EstateEvalInfo _EstateEvalInfo, Channel channel) {
        return LoanRequest.valueOf(regCode, mobile, _EstateEvalInfo, channel, Constants.REQUEST_SOURCE_ZXY);
    }

    public static LoanRequest valueOf(String regCode, String mobile, EstateEvalInfo _EstateEvalInfo, Channel channel, int source) {
        LoanRequest loanRequest = new LoanRequest();
        loanRequest.setEstateEvalId(_EstateEvalInfo.getId());
        loanRequest.setArea(regCode);
        loanRequest.setMobile(mobile);
        if (channel != null) {
            loanRequest.setChannelId(channel.getId());
            loanRequest.setChannelNo(channel.getNo().substring(0, 4));
        }
        if (channel != null && !Constants.CHANNEL_PARENT.equals(channel.getPid())) {
            loanRequest.setManagerId(channel.getUserId());
        }
        loanRequest.setStatus(Constants.REQUEST_STATUS_WAIT);
        loanRequest.setSource(source);
        loanRequest.setEstateEvalId(_EstateEvalInfo.getId());
        return loanRequest;
    }

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "estateEvalId", referencedColumnName = "id", insertable = false, nullable = true, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    public EstateEvalInfo getEstateEvalInfo() {
        return estateEvalInfo;
    }

    public void setEstateEvalInfo(EstateEvalInfo estateEvalInfo) {
        this.estateEvalInfo = estateEvalInfo;
    }

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "channelId", referencedColumnName = "id", insertable = false, nullable = true, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "managerId", referencedColumnName = "id", insertable = false, nullable = true, updatable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getEstateEvalId() {
        return estateEvalId;
    }

    public void setEstateEvalId(String estateEvalId) {
        this.estateEvalId = estateEvalId;
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public BigDecimal getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(BigDecimal loanAmount) {
        this.loanAmount = loanAmount;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getManagerId() {
        return managerId;
    }

    public void setManagerId(String managerId) {
        this.managerId = managerId;
    }

    @Column(
            updatable = false
    )
    public Integer getSource() {
        return source;
    }

    public void setSource(Integer source) {
        this.source = source;
    }

    public Date getBoughtAt() {
        return boughtAt;
    }

    public void setBoughtAt(Date boughtAt) {
        this.boughtAt = boughtAt;
    }

    public Integer getPropertyCoOwner() {
        return propertyCoOwner;
    }

    public void setPropertyCoOwner(Integer propertyCoOwner) {
        this.propertyCoOwner = propertyCoOwner;
    }

    public Integer getIsOnlyHouse() {
        return isOnlyHouse;
    }

    public void setIsOnlyHouse(Integer isOnlyHouse) {
        this.isOnlyHouse = isOnlyHouse;
    }

    public Integer getMortgageStatus() {
        return mortgageStatus;
    }

    public void setMortgageStatus(Integer mortgageStatus) {
        this.mortgageStatus = mortgageStatus;
    }

    public Integer getUseStatus() {
        return useStatus;
    }

    public void setUseStatus(Integer useStatus) {
        this.useStatus = useStatus;
    }

    public Integer getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(Integer maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public Integer getIsOverdue() {
        return isOverdue;
    }

    public void setIsOverdue(Integer isOverdue) {
        this.isOverdue = isOverdue;
    }

    public Integer getOverdueCount() {
        return overdueCount;
    }

    public void setOverdueCount(Integer overdueCount) {
        this.overdueCount = overdueCount;
    }

    public String getFundUse() {
        return fundUse;
    }

    public void setFundUse(String fundUse) {
        this.fundUse = fundUse;
    }

    public String getChannelNo() {
        return channelNo;
    }

    public void setChannelNo(String channelNo) {
        this.channelNo = channelNo;
    }

    public String getTelSaleManager() {
        return telSaleManager;
    }

    public void setTelSaleManager(String telSaleManager) {
        this.telSaleManager = telSaleManager;
    }
}
